/*
package cn.micai.queue;

import java.io.File;
import java.io.FileFilter;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

*/
/**
 * 桌面搜索应用程序中的生产者和消费者
 *//*

public class FileCrawler implements Runnable {

    private final BlockingQueue<File> fileQueue;
    private final FileFilter fileFilter;
    private final File root;

    public FileCrawler(BlockingQueue<File> fileQueue, FileFilter fileFilter, File root) {
        this.fileQueue = fileQueue;
        this.fileFilter = fileFilter;
        this.root = root;
    }

    @Override
    public void run() {
        try {
            crawl(root);
        } catch (Exception e) {
            e.printStackTrace();
            // 当前线程中断
            Thread.currentThread().interrupt();
        }
    }

    private void crawl(File root) {
        File[] files = root.listFiles(fileFilter);
        try {
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        crawl(file);
                    } else if (!alreadyIndexed(file)) {
                        fileQueue.put(file);
                    }
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public class Indexer implements Runnable {

        private final BlockingQueue<File> queue;

        public Indexer(BlockingQueue<File> queue) {
            this.queue = queue;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    indexFile(queue.take());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }
        }

    }

    public static void startIndexing(File [] roots) {
        BlockingQueue<File> queue = new LinkedBlockingQueue<>(BOUND);
        FileFilter fileFilter = new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return true;
            }
        };
        for (File file : roots) {
            new Thread(new FileCrawler(queue, fileFilter, file)).start();
        }
        for (int i=0; i<N_CONSUMERS; i++) {
            new Thread(new Indexer(queue)).start();
        }
    }


}
*/
